/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package framework.config;

import framework.data.Usuario;
import framework.db.UsuarioManager;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.util.Properties;

/**
 *
 * @author du
 */
public class PropertiesManager
{
    private Properties properties = null;
    private static PropertiesManager instance = null;
    File file = null;

    public static PropertiesManager getInstance()
    {
        if ( instance == null )
        {
            instance = new PropertiesManager();
        }

        return instance;
    }

    /**
     * PropertiesManager
     *
     */
    private PropertiesManager()
    {
        this.file = FileManager.getInstance().getConfigFile();

        if ( properties == null )
        {
            try
            {
                properties = new Properties();
                properties.load( new FileReader( file ) );
            }

            catch( Exception e )
            {
                Utilities.handleException( e );
            }
        }
    }

    /**
     * addProperty
     *
     * @param key String
     * @param property String
     */
    public void addProperty( String key, String property )
    {
        properties.setProperty( key, property );
    }

    /**
     * addProperty
     *
     * @param key String
     * @param property int
     */
    public void addProperty( String key, int property )
    {
        addProperty( key, String.valueOf( property ) );
    }

    /**
     * addProperty
     *
     * @param key String
     * @param property boolean
     */
    public void addProperty( String key, boolean property )
    {
        addProperty( key, Boolean.toString( property ) );
    }

    /**
     * getString
     *
     * @param key String
     * @return String
     */
    public String getString( String key )
    {
        return getString( key, null );
    }

    /**
     * getString
     *
     * @param key String
     * @param def String
     * @return String
     */
    public String getString( String key, String def )
    {
        return properties.getProperty( key, def );
    }

    /**
     * getInt
     *
     * @param key String
     * @return int
     */
    public int getInt( String key )
    {
        return Integer.parseInt( getString( key ) );
    }
    
    /**
     * getInt
     * 
     * @param key String
     * @param def int
     * @return int
     */
    public int getInt( String key, int def )
    {
        return Integer.parseInt( getString( key, String.valueOf( def ) ) );
    }

    /**
     * getBoolean
     *
     * @param key String
     * @return boolean
     */
    public boolean getBoolean( String key )
    {
        return Boolean.parseBoolean( getString( key ) );
    }

    /**
     * getBoolean
     *
     * @param key String
     * @param def boolean
     * @return boolean
     */
    public boolean getBoolean( String key, boolean def )
    {
        return Boolean.parseBoolean( getString( key, Boolean.toString( def ) ) );
    }

    /**
     * saveProperties
     *
     */
    public void saveProperties()
    {
        try
        {
            properties.store( new FileWriter( file ), "Properties File for Guest" );
            System.out.println( file );
        }

        catch( Exception e )
        {
            Utilities.handleException( e );
        }
    }

    /**
     * loadProperties
     *
     */
    public void loadProperties()
    {
        try
        {
            properties.load( new FileReader( file ) );
        }

        catch( Exception e )
        {
            Utilities.handleException( e );
        }
    }
    
    public static void main( String[] args )
    {
        try
        {
            Usuario usuario = new Usuario();
            usuario.setCodigo( 1 );
            usuario.setLogin( "hello world" );
            usuario.setSenha( "teste" );
            
            UsuarioManager.getInstance().addUsuario( usuario );
        }
        
        catch ( Exception e )
        {
            e.printStackTrace();
        }
    }
}
